package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.FenXiangJl;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lyli on 2017/11/25.
 */

public class ShareJlAdapter extends BaseAdapter {


    private Context context;
    private List<FenXiangJl.SharerecordsBean> dataList = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;

    public ShareJlAdapter(Context context, List<FenXiangJl.SharerecordsBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);
        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }


    public void addRes(List<FenXiangJl.SharerecordsBean> data) {
        if (data != null) {
//            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        FenXiangJl.SharerecordsBean bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.fenxiangjilu_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.fenxiangTitle.setText(bean.getMytitle());
        viewHolder.lessonTitle.setText(bean.getTitle());
        viewHolder.lessonPrice.setText("价格：" + bean.getPrice());
        viewHolder.lessonFan.setText(bean.getFandian());
        viewHolder.lessonMoney.setText("¥" + bean.getYongjin());


        viewHolder.dianji.setText(bean.getClick() + "");
        viewHolder.toumai.setText(bean.getMall() + "");
        viewHolder.daozhangyongjin.setText("¥" + bean.getYongjined());

        imageLoader.displayImage(bean.getLargePicture(), viewHolder.lessonImg, options);


        return view;
    }


    class ViewHolder {
        @BindView(R.id.fenxiang_title)
        TextView fenxiangTitle;
        @BindView(R.id.lesson_img)
        ImageView lessonImg;
        @BindView(R.id.lesson_type)
        TextView lessonType;
        @BindView(R.id.lesson_title)
        TextView lessonTitle;
        @BindView(R.id.lesson_price)
        TextView lessonPrice;
        @BindView(R.id.lesson_fan)
        TextView lessonFan;
        @BindView(R.id.lesson_yongjin)
        TextView lessonYongjin;
        @BindView(R.id.lesson_money)
        TextView lessonMoney;
        @BindView(R.id.dianji)
        TextView dianji;
        @BindView(R.id.toumai)
        TextView toumai;
        @BindView(R.id.daozhangyongjin)
        TextView daozhangyongjin;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
